package com.vshop.controller.optimize;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vshop.entity.comentity.UserOrderPackage;
import com.vshop.entity.interfacerequest.ExceptionRequest;
import com.vshop.entity.tradingrecord.request.RequestTransactionRecord;
import com.vshop.entity.tradingrecord.response.ResponseTransactionRecord;
import com.vshop.service.UserOrderPackageService;
import com.vshop.service.impl.PurchaseLogicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 根据iccid，时间查询支付交易记录，需要获取以下字段：
 * 1，支付流水
 * 2，支付币种
 * 3，支付金额
 * 4，支付请求时间
 * 5，支付完成时间
 * 6，客户卡片ICCID
 * 7，支付结果
 * 8，支付失败原因
 * 9，购买套餐编码
 * 10，购买周期
 *
 * @author lm
 * @date 2020/2/10
 */

@RestController
@Api(description = "支付交易记录查询")
@Slf4j
public class TransactionRecordQueryController {
    @Autowired
    UserOrderPackageService orderService;

    @Autowired
    PurchaseLogicService purchaseService;

    @CrossOrigin
    @PostMapping(value = "transactionRecord", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "支付交易记录查询", response = ResponseTransactionRecord.class)
    public String transactionRecord(@ApiParam @RequestBody RequestTransactionRecord record) {

        if (StringUtils.isNotBlank(JSON.toJSONString(record))) {
            log.info("【支付交易记录查询】请求：" + JSON.toJSONString(record));
            RequestTransactionRecord re = JSONArray.parseObject(JSON.toJSONString(record), RequestTransactionRecord.class);

            if (null != re.getDeviceCode() && null != re.getPayId() && null != re.getStartDate() && null != re.getEndDate()) {

                List<UserOrderPackage> orderList = orderService.queryRecord(re.getDeviceCode(), re.getPayId(), re.getStartDate(), re.getEndDate());

                List<ResponseTransactionRecord> responseList = null;

                if (null != orderList && orderList.size() > 0) {

                    responseList = new ArrayList<ResponseTransactionRecord>();

                    ResponseTransactionRecord response = null;

                    for (UserOrderPackage o : orderList) {

                        response = new ResponseTransactionRecord();

                        if (StringUtils.isNotBlank(o.getOrderId())) {
                            response.setOrderId(o.getOrderId());
                        } else {
                            response.setOrderId("");
                        }
                        response.setPayId(o.getPayId());
                        response.setPayCurrency(o.getPayCurrency());
                        response.setPayAmount(o.getPayAmount());
                        response.setDeviceCode(o.getDeviceCode());
                        String email = purchaseService.queryEmail(o.getDeviceCode());
                        if (StringUtils.isNotBlank(email)) {
                            response.setEmail(email);
                        } else {
                            response.setEmail("");
                        }
                        response.setPayRst(o.getPayRst());
                        response.setFailReason("");
                        response.setPackageCode(o.getPackageCode());
                        response.setPackageName(o.getPackageName());
                        response.setPartnerName(o.getPartnerName());
                        response.setOrderPeriod(o.getOrderPeriod());
                        try {
                            response.setRequestDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o.getCreateDate()))));
                            response.setPayDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o.getCreateDate()))));
                        } catch (ParseException e) {
                            log.info("时间转换异常：" + e);
                        }

                        responseList.add(response);
                    }

                }
                // 返回给调用者的报文
                String jsonString = JSON.toJSONString(responseList);
                log.info("【支付交易记录查询】响应：" + jsonString);
                return jsonString;

            } else {
                return ExceptionRequest.responseStr("9998", "请求参数不合法");
            }

        } else {
            return ExceptionRequest.responseStr("9998", "请求报文为空");
        }
    }

}
